package waitTest;

import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;
import java.util.concurrent.locks.ReentrantLock;

/**
 * todo
 *
 * @author: luolidong
 * @create: 2021-07-09 16:06
 * @version: 1.0
 */
public class Test {
    boolean flag = true;

    public synchronized void get() throws InterruptedException {
        while (flag==true) {
            //等待，释放当前调用对象的锁,有参数在指定时间没有唤醒，就不在等待
            System.out.println("wait()方法之前代码");
            this.wait();
            System.out.println("wait()方法之后代码");

        }
        System.out.println(Thread.currentThread().getName() + ": 111");
        flag = true;
        //唤醒,唤醒正在等待对象监视器的单个线程。
        this.notify();


    }

    public synchronized void get1() throws InterruptedException {
        while (flag==false) {
            this.wait();
        }
        System.out.println(Thread.currentThread().getName() + ": 222");
        flag = false;
        //唤醒
        this.notify();


    }

    public static void main(String[] args) {
        Test test = new Test();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    test.get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    test.get1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
//
//        AbstractQueuedLongSynchronizer;
//        ReentrantLock;
//        ThreadLocal
    }
}
